$(function () {
    var matchUserName = false;
    var matchPassword = false;
    var matchEmail = false;
    var emailPattern = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$";
    var matchPhone = false;
    var existEmail = false;

    $("#name").blur(function () {
        var userName = $("#name").val();
        if(userName != ""){
            $("#nullName").css("display","none");
            $.ajax({
                type: "GET",
                url: "/ifUserNameExist",
                data: {userName: userName},
                dataType: "json",
                success: function(data){
                    if (data.exist == true){
                        $("#matchName").css("display","inline");
                        matchUserName = false;
                    }else{
                        $("#matchName").css("display","none");
                        matchUserName = true;
                    }
                }
            });
        } else {
            $("#nullName").css("display","inline");
            matchUserName = false;
        }

    });

    $("#repassword").blur(function () {
        if ($("#password").val() == $("#repassword").val()){
            matchPassword = true;
            $("#matchPassword").css("display","none");
        }
        else{
            matchPassword = false;
            $("#matchPassword").css("display","inline");
        }
    })

    $("#password").blur(function () {
        if ($("#password").val() == $("#repassword").val()){
            matchPassword = true;
            $("#matchPassword").css("display","none");
        }
        else{
            matchPassword = false;
            $("#matchPassword").css("display","inline");
        }
    })

    $("#email").blur(function () {
        if($("#email").val().match(emailPattern)){
            matchEmail = true;
            $("#matchEmail").css("display","none");
            var emailVar = $("#email").val();
            $.ajax({
                type: "GET",
                url: "/ifEmailExist",
                data: {email: emailVar},
                dataType: "json",
                success: function(data){
                    if (data.exist == true){
                        $("#existEmail").css("display","inline");
                        existEmail = true;
                    }else{
                        $("#existEmail").css("display","none");
                        existEmail = false;
                    }
                }
            });
        }else {
            matchEmail = false;
            $("#matchEmail").css("display","inline");
        }
    })

    $("#phone").blur(function () {
        if($("#phone").val().match(/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/)){
            matchPhone = true;
            $("#matchPhone").css("display","none");
        }else {
            matchPhone = false;
            $("#matchPhone").css("display","inline");
        }
    })

    $("#add").click(function(){
        // alert($("#name").val());
        // alert($("#email").val());
        // alert($("#phone").val());
        // alert($('input:radio:checked').val());
        // alert($("#password").val());
        // alert($("#repassword").val());
        // alert($("#agree").is(':checked'));

        if($("#agree").is(':checked')){
            if(matchUserName && matchPassword && matchEmail && !existEmail && matchPhone){
                $("#add").submit();
            }else {
                return false;
            }
        }else {
            alert("please agree to website.")
            return false;
        }

    });
});